<template>
    <!-- pages/user/user.wxml -->
    <view class="container">
        <view class="top bg-primary">
            <!-- <navigator class="userInfo" url="../setUserInfo/setUserInfo"> -->
            <block v-if="isLogin">
                <button hover-class="button-click" class="userInfo" @tap="gotosetuserinfo">
                    <view class="left">
                        <view class="photo">
                            <image :src="userinfo.avatar " mode="widthFix"></image>
                        </view>
                        <view class="info">
                            <view class="name">昵称：{{ userinfo.nickname.length ? userinfo.nickname : '暂无' }}</view>
                            <view class="phone">手机：{{ userinfo.mobile }}</view>
                        </view>
                    </view>
                    <view class="iconfont icon-right"></view>
                </button>
            </block>
            <block v-else>
                <button hover-class="button-click" class="userInfo" @getphonenumber="phone" open-type="getPhoneNumber">
                    <view class="left">
                        <!-- <image class="photo" src="/logo.png"></image> -->
                        <!-- <button hover-class="button-click"  class="photo" src="/logo.png" bindgetphonenumber="phone" open-type="getPhoneNumber">地图</button> -->
                        <image class="photo" src="/static/logo.png"></image>
                        <view class="info">
                            <view class="name">点击登录</view>
                        </view>
                    </view>
                    <view class="iconfont icon-right"></view>
                </button>
            </block>
            <!-- 账户 -->
            <view class="account" v-if="isLogin">
                <navigator class="item" :url="isLogin ? '../getBalance/getBalance' : '../login/login'">
                    <view class="num">{{ userinfo.balance }}</view>
                    <view>总账户余额</view>
                </navigator>
                <navigator class="item" :url="isLogin ? '../getBalance/getBalance' : '../login/login'">
                    <view class="num">{{ userinfo.giftBalance }}</view>
                    <view>总赠送余额</view>
                </navigator>
                <navigator class="item" :url="isLogin ? '../coupon/coupon' : '../login/login'">
                    <view class="num">{{ userinfo.couponCount }}</view>
                    <view>优惠卡券</view>
                </navigator>
            </view>
        </view>
        <!-- 菜单列表 -->
        <view class="list-container">
            <view class="list">
                <view class="item" @tap="parseEventDynamicCode($event, isLogin ? 'goOrder' : 'gotologin')">
                    <view class="left">
                        <view class="icon iconfont icon-quanbudingdan color-primary"></view>
                        <text>我的订单</text>
                    </view>
                    <view class="iconfont icon-right"></view>
                </view>
                <navigator class="item" :url="isLogin ? '../myBalance/myBalance' : '../login/login'">
                    <view class="left">
                        <view class="icon iconfont icon-jiaoyi color-primary"></view>
                        <text>余额账单</text>
                    </view>
                    <view class="iconfont icon-right"></view>
                </navigator>
                <navigator class="item" url="../tuangou/tuangou">
                    <view class="left">
                        <view class="icon iconfont icon-tuangou color-primary"></view>
                        <text>团购验券</text>
                    </view>
                    <view class="iconfont icon-right"></view>
                </navigator>
                <navigator class="item" url="../help/help">
                    <view class="left">
                        <view class="icon iconfont icon-help color-primary"></view>
                        <text>使用帮助</text>
                    </view>
                    <view class="iconfont icon-right"></view>
                </navigator>
                <!-- <navigator class="item" url="../index/index" open-type="switchTab">
      <view class="left">
        <view class="icon iconfont icon-door color-primary"></view>
        <text>全部门店</text>
      </view>
      <view class="iconfont icon-right"></view>
    </navigator> -->
                <navigator class="item" :url="isLogin ? '../join/join' : '../login/login'">
                    <view class="left">
                        <view class="icon iconfont icon-hezuo color-primary"></view>
                        <text>我要加盟</text>
                    </view>
                    <view class="iconfont icon-right"></view>
                </navigator>
            </view>
        </view>
        <!-- 按钮菜单 -->
        <block v-if="isLogin && userinfo.userType != 11">
            <view class="menu">
                <view class="title border-primary">功能专区</view>
                <view class="box" v-if="userinfo.userType != 14">
                    <navigator class="item" url="../../packageA/pages/setStore/setStore">
                        <view class="iconfont icon-mendianguanli color-primary"></view>
                        <view>门店管理</view>
                    </navigator>
                    <navigator class="item" url="/packageA/pages/roomList/roomList">
                        <view class="iconfont icon-wanle color-primary"></view>
                        <view>房间控制</view>
                    </navigator>
                    <navigator class="item" url="/packageA/pages/SetOrder/SetOrder">
                        <view class="iconfont icon-dingdan color-primary"></view>
                        <view>订单管理</view>
                    </navigator>
                    <navigator class="item" url="/packageA/pages/setVip/setVip">
                        <view class="iconfont icon-huiyuan color-primary"></view>
                        <view>会员管理</view>
                    </navigator>
                    <navigator class="item" url="/packageA/pages/setCoupon/setCoupon">
                        <view class="iconfont icon-diyongquan color-primary"></view>
                        <view>优惠卡券</view>
                    </navigator>
                    <navigator class="item" url="/packageA/pages/scanQr/scanQr">
                        <view class="iconfont icon-qr-code color-primary"></view>
                        <view>团购验券</view>
                    </navigator>
                    <navigator class="item" url="/packageA/pages/admin/admin">
                        <view class="iconfont icon-employee color-primary"></view>
                        <view>员工管理</view>
                    </navigator>
                    <navigator class="item" url="/packageA/pages/cleaner/cleaner">
                        <view class="iconfont icon-renyuanguanli color-primary"></view>
                        <view>保洁员管理</view>
                    </navigator>
                    <navigator class="item" url="/packageA/pages/taskManager/taskManager">
                        <view class="iconfont icon-dingdan2 color-primary"></view>
                        <view>保洁订单</view>
                    </navigator>
                    <navigator class="item" url="/packageA/pages/statics/statics">
                        <view class="iconfont icon-tongji color-primary"></view>
                        <view>数据统计</view>
                    </navigator>
                    <navigator class="item" url="/packageA/pages/packageManagement/packageManagement">
                        <view class="iconfont icon-taocanguanli color-primary"></view>
                        <view>套餐管理</view>
                    </navigator>
                    <navigator class="item" url="/packageA/pages/deviceList/deviceList">
                        <view class="iconfont icon-device color-primary"></view>
                        <view>设备管理</view>
                    </navigator>
                </view>
                <view class="box" v-else>
                    <!-- 保洁端 -->
                    <navigator class="item" url="/packageA/pages/task/task">
                        <view class="iconfont icon-renwu color-primary"></view>
                        <view>任务大厅</view>
                    </navigator>
                    <navigator class="item" url="/packageA/pages/roomList/roomList">
                        <view class="iconfont icon-wanle color-primary"></view>
                        <view>房间状态</view>
                    </navigator>
                    <navigator class="item" url="/packageA/pages/taskStatics/taskStatics">
                        <view class="iconfont icon-tongji color-primary"></view>
                        <view>任务统计</view>
                    </navigator>
                </view>
            </view>
        </block>
    </view>
</template>

<script>
// pages/user/user.js
var http = require('../../utils/http');
var util1 = require('../../utils/util.js');
const app = getApp();
export default {
    data() {
        return {
            isLogin: app.globalData.isLogin,
            //用户信息
            userinfo: {
                couponCount: 0,
                giftBalance: 0,
                balance: 0,
                avatar: false,
                nickname: [],
                mobile: '',
                userType: 0
            },
            couponCount: 0,
            giftBalance: 0,
            balance: 0
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {},
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let that = this;
        that.setData({
            isLogin: app.globalData.isLogin
        });
        if (!app.globalData.isLogin) {
            that.setData({
                couponCount: 0,
                giftBalance: 0,
                balance: 0
            });
        }
        that.getuserinfo();
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    methods: {
        phone: function (e) {
            //console.log("授权用户手机号");
            var that = this;
            if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
                uni.showToast({
                    title: '已取消授权'
                });
            }
            if (e.detail.errMsg == 'getPhoneNumber:ok') {
                //console.log('手机号码授权+++++++');
                //console.log(e.detail);
                //console.log('手机号码授权+++++++');
                uni.login({
                    success: function (res) {
                        //console.log('111++++==');
                        //console.log(res);
                        //console.log('111++++==');
                        if (res.code != null) {
                            http.request(
                                '/member/auth/weixin-mini-app-login',
                                '1',
                                'post',
                                {
                                    phoneCode: e.detail.code,
                                    loginCode: res.code
                                },
                                '',
                                '获取中...',
                                function success(info) {
                                    console.info('返回111===');
                                    console.info(info);
                                    if (info.code == 0) {
                                        if (info.data) {
                                            app.globalData.userDatatoken = info.data;
                                            app.globalData.isLogin = true;
                                            that.setData({
                                                isLogin: true
                                            });
                                            //缓存服务器返回的用户信息
                                            uni.setStorageSync('userDatatoken', info.data);
                                            that.getuserinfo();
                                        }
                                    }
                                },
                                function fail(info) {}
                            );
                        } else {
                            //console.log('登录失败！' + res.errMsg)
                        }
                    }
                });
            }
        },

        getuserinfo: function () {
            var that = this;
            if (app.globalData.isLogin) {
                http.request(
                    '/member/user/get',
                    '1',
                    'get',
                    {},
                    app.globalData.userDatatoken.accessToken,
                    '',
                    function success(info) {
                        console.info('我的信息===');
                        console.info(info);
                        if (info.code == 0) {
                            that.setData({
                                userinfo: info.data
                            });
                        }
                    },
                    function fail(info) {}
                );
            } else {
                //console.log('未登录失败！')
            }
        },

        gotosetuserinfo: function () {
            uni.navigateTo({
                url: '../setUserInfo/setUserInfo'
            });
        },

        goOrder: function () {
            uni.switchTab({
                url: '../orderList/orderList'
            });
        },

        //到登录界面
        gotologin() {
            uni.navigateTo({
                url: '../login/login'
            });
        }
    }
};
</script>
<style>
@import './user.css';
</style>
